package com.qk.Dao;

import java.util.List;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.ibatis.session.SqlSession;

import com.qk.my.pojos.Player;

public class PlayerDao {
	private static SqlSession session;
	private static String statement;
	static {
		session = SqlSessionDao.getSession();
	}

	public static List<Player> getAllPlayer() {
		statement = "com.qk.mapping.Playermapper.getAllPlayer";
		List<Player> players = session.selectList(statement);
		return players;
	}

	public static int insertPlayer(Player player) {
		statement = "com.qk.mapping.Playermapper.insertPlayer";
		player.setPassword(DigestUtils.md5Hex(player.getPassword()));
		int i = session.insert(statement, player);
		session.commit();
		return i;
	}

	public static int updatePlayer(Player player) {
		statement = "com.qk.mapping.Playermapper.updatePlayer";
		int i = session.update(statement, player);
		session.commit();
		return i;
	}

	public static Player getPlayerById(int id) {
		statement = "com.qk.mapping.Playermapper.getPlayerById";
		Player player = session.selectOne(statement, id);
		return player;
	}

	public static boolean isExist(String username) {
		statement = "com.qk.mapping.Playermapper.isExist";
		Player player = session.selectOne(statement, username);
		if (player == null) {
			return false;
		}
		return true;
	}

	public static Player toLogin(String username, String password) {
		statement = "com.qk.mapping.Playermapper.toLogin";
		Player player = new Player();
		player.setUsername(username);
		player.setPassword(DigestUtils.md5Hex(password));
		player = session.selectOne(statement, player);
		return player;
	}

}
